/*
 * Copyright (c) 2019 Nanjing Wind Information Co., Ltd. All Rights Reserved;
 * @Package com.wind.autotest.utils
 * @version 1.0.0
 * @Date 2019年2月3日
 * @author jlliu.Farrior@wind.com.cn
 */
package wind.autotest.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @Description: <类功能描述>
 * @author jlliu.Farrior@wind.com.cn
 * @Date 2019年2月3日 上午11:20:23
 * 
 */
@Slf4j
public class FileUtil {
	
	public static List<String> getSampleRecordIds(InputStream is) {
		if (null == is) {
			return null;
		}
		List<String> samples = new LinkedList<>();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is,
					"UTF-8"));
			String tmpline = null;
			while ((tmpline = br.readLine()) != null) {
				samples.add(tmpline);
			}
		} catch (Exception e) {
			log.info("读取上传样本记录ID异常:{}", ExceptionUtils.getStackTrace(e));
		}
		return samples;
	}

	/**
	 * 写文件
	 * 
	 * @param record
	 * @param fileName
	 * @throws IOException
	 */
	public static void writeFile(String record, String path, String fileName,
			boolean append) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		String fullpath = path + File.separator + fileName;
		BufferedWriter f = null;
		try {
			f = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
					fullpath, append), "UTF-8"));
			f.write(record);
			f.newLine();
			f.flush();
			f.close();
		} catch (IOException e) {
			log.error("写文件异常：{}", ExceptionUtils.getFullStackTrace(e));
		}
	}

	public static void writeFile(String record, String path, String fileName) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		String fullpath = path + File.separator + fileName;
		BufferedWriter f = null;
		try {
			f = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
					fullpath, false), "UTF-8"));
			f.write(record);
			f.newLine();
			f.flush();
			f.close();
		} catch (IOException e) {
			log.error("写文件异常：{}", ExceptionUtils.getFullStackTrace(e));
		}
	}

	/**
	 * 
	 * 功能描述:<读取json串文件>
	 * @param fullpath
	 * @return 
	 * @author jlliu.Farrior
	 */
	public static JSONObject readJsonFile(String fullpath) {
		StringBuffer jsonText = new StringBuffer();
		String tmpStr = "";
		JSONObject jobj = null;
		BufferedReader br = null;
		try {
			// 读取原始json文件
			br = new BufferedReader(new FileReader(fullpath));
			while ((tmpStr = br.readLine()) != null) {
				jsonText.append(tmpStr.trim());
			}
			jobj = JSON.parseObject(jsonText.toString());
			br.close();
		} catch (Exception e) {
			log.error("json文件读取错误:{}", ExceptionUtils.getFullStackTrace(e));
		}
		return jobj;
	}

	/**
	 * 
	 * 功能描述:<读取json数组文件>
	 * @param fullpath
	 * @return 
	 * @author jlliu.Farrior
	 */
	public static JSONArray readJsArrayFile(String fullpath) {
		
		File f = new File(fullpath);
		if (!f.exists()) {
			return null;
		}
		StringBuffer jsonText = new StringBuffer();
		String tmpStr = "";
		JSONArray jobj = null;
		BufferedReader br = null;
		try {
			// 读取原始json文件
			br = new BufferedReader(new FileReader(fullpath));
			while ((tmpStr = br.readLine()) != null) {
				jsonText.append(tmpStr.trim());
			}
			jobj = JSONArray.parseArray(jsonText.toString());
			br.close();
		} catch (Exception e) {
			log.error("json文件读取错误:{}", ExceptionUtils.getFullStackTrace(e));
		}
		return jobj;
	}

}
